package ija.view;

import ija.controller.CheckersController;
import ija.controller.GameController;
import ija.controller.types.GameState;
import ija.model.basis.Desk;
import ija.model.history.History;
import ija.model.history.HistoryMove;
import ija.model.players.LocalPlayer;
import ija.model.players.Player;
import ija.model.types.FigureColor;

import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

public class CheckersContent extends BoardGameContent implements Observer {

    private static final long serialVersionUID = 6866873030477942289L;
    protected Desk dDesk;
    protected CheckersController controller;

    public CheckersContent(CheckersController gc) {
        this.setController(gc);
        this.addWidgets(gc);
        this.getController().setView(this);
        this.getController().getModel().addObserver(this);
        this.getController().initModel(FigureColor.BLACK);
        this.setState(GameState.GAMESETTINGS);
    }

    @Override
    public void setState(GameState gs) {
        super.setState(gs);

        this.pPlayer1.setNetState(gs);
        this.pPlayer2.setNetState(gs);

        if (gs == GameState.GAMEINPROGRESS) {
            this.pBoard.setEnabled(true);
        } else {
            this.pBoard.setEnabled(false);
        }
    }

    @Override
    protected void addWidgets(GameController gc) {
        super.addWidgets(gc);
        this.pPlayer1.addHostListener(this.controller.createHostListener());
        this.pPlayer1.addJoinListener(this.controller.createJoinListener());
        this.pPlayer2.addHostListener(this.controller.createHostListener());
        this.pPlayer2.addJoinListener(this.controller.createJoinListener());
    }

    public void setController(CheckersController gc) {
        this.controller = gc;
    }

    @Override
    public CheckersController getController() {
        return this.controller;
    }

    @Override
    public void update(Observable o, Object obj) {
        System.out.println(obj.getClass().toString());
        if (obj instanceof Player[]) {
            Player players[] = (Player[]) obj;
            if (players[0] != null) {
                this.pPlayer1.setFromPlayer(players[0]);
            }
            if (players[1] != null) {
                this.pPlayer2.setFromPlayer(players[1]);
            }
        }
        if (obj instanceof Desk) {
            Desk desk = (Desk) obj;
            this.pBoard.setFromDesk(desk);
        }

        if (obj instanceof String) {
            this.lMessage.setText((String) obj);
        }

        if (obj instanceof Player) {
            boolean isLocal = (obj instanceof LocalPlayer);
            this.pBoard.refresh(((Player) obj).getColor(), isLocal);
        }
        if (obj instanceof History) {
            History history = (History) obj;
            this.taMoveHistory.setText("");
            Iterator<HistoryMove> hmI = history.iterator();
            while (hmI.hasNext()) {
                HistoryMove hm = hmI.next();
                String round = this.controller.getNotationReader().translateRoundBack(hm) + "\n";
                this.taMoveHistory.append(round);
            }
        }
    }
}
